package shopping.shiro;

import java.util.List;

import javax.annotation.Resource;

import org.apache.shiro.config.Ini;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.util.CollectionUtils;
import org.apache.shiro.web.config.IniFilterChainResolverFactory;

import shopping.dao.IMenuDao;
import shopping.entity.Menu;

public class MyShiroFilterFactoryBean extends ShiroFilterFactoryBean {

	/**配置中的过滤链*/
	public static String definitions;
	
	@Resource
	IMenuDao menuDao;
	
	@Override
	public void setFilterChainDefinitions(String definitions) {
		
		StringBuffer buffer = new StringBuffer(definitions);
		MyShiroFilterFactoryBean.definitions = definitions;
        
		List<Menu> menus = menuDao.findByHql(Menu.class, null);
		if(null == menus || menus.isEmpty())
			return;
		
       for(Menu menu:menus){
    	   buffer.append(menu.getUrl()).append("=perms[").append(menu.getUrl()).append("]");
       }
		//从配置文件加载权限配置
		Ini ini = new Ini();
        ini.load(buffer.toString());
        Ini.Section section = ini.getSection(IniFilterChainResolverFactory.URLS);
        if (CollectionUtils.isEmpty(section)) {
            section = ini.getSection(Ini.DEFAULT_SECTION_NAME);
        }
        setFilterChainDefinitionMap(section);
	}
}
